<?php

class Module_Admin_Controller_Catalog extends Engine_Controller {

    protected $menu = Engine_Data_Model_Menu::PARENT_ADMIN;
    protected $name = 'Администрирование';

    public function preDispatch() {
        parent::preDispatch();
        $this->activeUser->checkPermission('module.admin');
    }

    public function indexAction(){
        $this->authUserOnly();
    }

    public function usersAction() {
        $this->authUserOnly();
    	$gridAdapter = new Module_Admin_Grid_User_Adapter();
    	$grid = new Module_Admin_Grid_User($gridAdapter);
        $this->getView()->grid = $grid;

    }

    public function rolesAction(){
        $this->authUserOnly();
        $gridAdapter= new Module_Admin_Grid_Role_Adapter();
        $grid = new Module_Admin_Grid_Role($gridAdapter);
        $this->getView()->grid = $grid;
    }

    public function adduserAction() {
        $this->authUserOnly();
        $tRole= new Engine_Data_Table_Role();
        $tRole->selectAll(null);
        $form= new Module_Admin_Form_User('adduser', array('name' => 'Добавить пользователя'));
        $element = $form->getElement('role');
        $element->setOptions($tRole->GetRolesForselect());
        $this->getView()->form = $form;
        $params = $this->getRequest()->getParams();
        $form->populate($params);
        if ($_POST && $form->isValid()){
            $data= array(
                'login'     =>$params['login'],
                'password'  =>sha1($params['password']),
                'statusId'  =>0,
                'role'      =>$params['role'],
                'firstname' =>$params['firstname'],
                'lastname'  =>$params['lastname']
            );
            $mUser= new Module_Account_Model_User();
            $mUser->addUser($data);
            $this->getRequest()->redirect('/admin/catalog/users/');
        }
    }

    public function edituserAction(){
        $this->authUserOnly();
        $id = $this->getRequest()->getParam('id', false);
        if (!$id) {
            throw new Exception("id not found", 404);
        }
        $tRole= new Engine_Data_Table_User();
        $data=$tRole->getById($id);
        if (!$data) {
            throw new Exception("User not found", 404);
        }
        $form= new Module_Admin_Form_User('edituser', array('name'=>'Редактировать пользователя'));
        $tRole= new Engine_Data_Table_Role();
        $tRole->selectAll(null);
        $form->getElement('role')->setOptions($tRole->GetRolesForselect());
        $form->populate($data);
        $form->removeElement('password');
        $form->removeElement('accept_password');

        $params = $this->getRequest()->getParams();
        if ($_POST && $form->isValid($params)){
            $changedData= array(
                'login'     =>$params['login'],
                'role'      =>$params['role'],
                'firstname' =>$params['firstname'],
                'lastname'  =>$params['lastname']
            );
            $tUser= new Engine_Data_Table_User();
            $tUser->update($changedData,'id='. $id. '');
            $this->getRequest()->redirect('/admin/catalog/users/');
        }
        $this->getView()->form = $form;
    }

    public function userProfileAction(){
        $this->authUserOnly();
        $id = $this->getRequest()->getParam('id', false);
        if (!$id) {
            throw new Exception("id not found", 404);
        }
        $tRole= new Engine_Data_Table_User();
        $data=$tRole->getById($id);
        $form= new Module_Admin_Form_User('userProfile', array('name'=>'Профиль пользователя'));
        $tRole= new Engine_Data_Table_Role();
        $tRole->selectAll(null);
        $form->getelement('role')->setOptions($tRole->getRolesForSelect());
        $form->getElement('role')->setAttrib('disabled','role');
        $form->removeElement('password');
        $form->removeElement('accept_password');
        $form->removeElement('submit');
        $form->getElement('firstname')->setAttrib('disabled','firstname');
        $form->getElement('lastname')->setAttrib('disabled','lastname');
        $form->getElement('login')->setAttrib('disabled','login');
        $form->populate($data);
        $this->getView()->form=$form;

    }

    public function permissionAction(){
        $this->authUserOnly();
        $gridAdapter= new Module_Admin_Grid_Permission_Adapter();
        $this->getView()->grid = $grid;
    }

    public function editRoleAction(){
        $this->authUserOnly();
        $id = $this->getRequest()->getParam('id', false);
        if (!$id) {
            throw new Exception("id not found", 404);
        }
        $form= new Module_Admin_Form_Role ('editrole', array('name'=>'Редактирование роли'));
        $this->getView()->form=$form;
    }
}